﻿using System;
using HK.Core.Common.Data;
using HK.Core.Common.Protocol.Data;
using HK.Core.GUIEx.Protocol.Attribute;
using HK.Core.Serializable.Attribute;
using HK.Core.Serializable.Data;
using HK.Core.Serializable.Protocol.Data;
using HK.Editor.Settings.Protocol.Data;
using UnityEngine;

namespace HK.Editor.Settings.Data
{
    /// <summary>
    /// 打包资源版本配置
    /// </summary>
    [Serializable]
    public sealed class BuildResourceConf : JsonData<BuildResourceConf>, IBuildResourceConf
    {

#region Create

        /// <summary>
        /// 创建打包资源版本配置
        /// </summary>
        /// <returns>打包资源版本配置</returns>
        public static BuildResourceConf Create() => new BuildResourceConf();

#endregion
        
        /// <summary>
        /// 保护构造函数
        /// </summary>
        private BuildResourceConf() {}
        
#region Version
        
        [SerializeField, SVersion("母包版本", (int)GUIOperateOptions.ExpandableEdit)]
        private SAssetVersion originVersion = SAssetVersion.Create();
        /// <summary>
        /// 母包版本
        /// </summary>
        public ISAssetVersion OriginVersion => originVersion;

        /// <summary>
        /// 重置母包版本
        /// </summary>
        /// <param name="iVersion">母包版本</param>
        /// <returns>true:有变化; false:无变化;</returns>
        public bool ResetOriginVersion(string iVersion)
        {
            var newVersion = SAssetVersion.Decode(iVersion);
            if (null == newVersion || !newVersion.Valid) return false;

            return originVersion.ApplyData(newVersion);
        }
        
        [SerializeField, SVersion("上一母包版本", (int)GUIOperateOptions.ExpandableEdit)]
        private SAssetVersion lastOriginVersion = SAssetVersion.Create();
        /// <summary>
        /// 上一母包版本
        /// </summary>
        public ISAssetVersion LastOriginVersion => lastOriginVersion;
        
        /// <summary>
        /// 重置上一次母包版本
        /// </summary>
        /// <param name="iVersion">母包版本</param>
        /// <returns>true:有变化; false:无变化;</returns>
        public bool ResetLastOriginVersion(string iVersion)
        {
            var newVersion = SAssetVersion.Decode(iVersion);
            if (null == newVersion || !newVersion.Valid) return false;

            return lastOriginVersion.ApplyData(newVersion);
        }
        
        [SerializeField, SVersion("补丁版本", (int)GUIOperateOptions.ExpandableEdit)]
        private SAssetVersion patchVersion = SAssetVersion.Create();
        /// <summary>
        /// 补丁版本
        /// </summary>
        public ISAssetVersion PatchVersion => patchVersion;
        
        /// <summary>
        /// 重置补丁版本
        /// </summary>
        /// <param name="iVersion">母包版本</param>
        /// <returns>true:有变化; false:无变化;</returns>
        public bool ResetPatchVersion(string iVersion)
        {
            var newVersion = SAssetVersion.Decode(iVersion);
            if (null == newVersion || !newVersion.Valid) return false;

            return patchVersion.ApplyData(newVersion);
        }
        
        [SerializeField, SVersion("上一补丁版本", (int)GUIOperateOptions.ExpandableEdit)]
        private SAssetVersion lastPatchVersion = SAssetVersion.Create();
        /// <summary>
        /// 上一补丁版本
        /// </summary>
        public ISAssetVersion LastPatchVersion => lastPatchVersion;
        
        /// <summary>
        /// 重置上一次母包版本
        /// </summary>
        /// <param name="iVersion">母包版本</param>
        /// <returns>true:有变化; false:无变化;</returns>
        public bool ResetLastPatchVersion(string iVersion)
        {
            var newVersion = SAssetVersion.Decode(iVersion);
            if (null == newVersion || !newVersion.Valid) return false;

            return lastPatchVersion.ApplyData(newVersion);
        }
        
#endregion

        /// <summary>
        /// 应用数据
        /// </summary>
        /// <param name="iData">数据</param>
        /// <param name="iImport">导入标志位(true:从相应的*.json文件导入; false:反之;)</param>
        /// <returns>true:有变更; false:无变更;</returns>
        public override bool ApplyData(IJsonData iData, bool iImport = false)
        {
            if (null == iData || !iData.Valid) return false;

            var dirty = base.ApplyData(iData, iImport);
            if (dirty && !iImport)
            {
                Dirty = true;
            }

            if (iData is IBuildConfData conf)
            {
                dirty = originVersion.ApplyData(conf.OriginVersion);
                if (dirty && !iImport)
                {
                    Dirty = true;
                }
                
                dirty = lastOriginVersion.ApplyData(conf.LastOriginVersion);
                if (dirty && !iImport)
                {
                    Dirty = true;
                }
                
                dirty = patchVersion.ApplyData(conf.PatchVersion);
                if (dirty && !iImport)
                {
                    Dirty = true;
                }
                
                dirty = lastPatchVersion.ApplyData(conf.LastOriginVersion);
                if (dirty && !iImport)
                {
                    Dirty = true;
                }
            }
            
            return Dirty;
        }
    
        /// <summary>
        /// 清空
        /// </summary>
        public override void Clear()
        {
            base.Clear();
            
            originVersion.Clear();
            lastOriginVersion.Clear();
            patchVersion.Clear();
            lastPatchVersion.Clear();
        }
    }
}
